import MyViewBase from "./MyViewBase";




//AdType 0抽屉 1底部Banner 2主界面2两个 3 好友都在玩 4热门推荐 5结算界面
export class AdConfigData {
    public AppId: string = "";
    public AdType: number = 0;
    public AppName: string = "";
    public AppIcon: string = "";
    public ShowHot: number = 1;
}




export default class UIManager {

    private static m_instance: UIManager;

    public static get Instance() {
        if (this.m_instance == null) {
            this.m_instance = new UIManager();
        }
        return this.m_instance;
    }

    private static sViewMap = {};

    constructor()  {
        this.Init();
    }

    private Init()  {

        this.InitInnerView();
    }

    
    InitInnerView() 
    {

    }


    //================pushView======================
    private m_pushviewMap: { [keykey: string]: MyViewBase; } = {};
    private m_pushviewStack: Array<MyViewBase> = [];

    //InnerView=================================
    private m_innerviewMap: { [keykey: string]: BaseInnerView; } = {};


    /// <summary>
    /// 把某个页面入栈，  把某个页面显示在界面上
    /// </summary>
    public PushView(viewname: string, param?: any) {
        if (this.m_pushviewStack == null)
            this.m_pushviewStack = [];

        //判断一下栈里面是否有页面
        if (this.m_pushviewStack.length > 0) {
            let topview: MyViewBase = this.m_pushviewStack[this.m_pushviewStack.length - 1];
            topview.OnPause();
        }
        let panel: MyViewBase = this.GetView(viewname);
        if (panel == null) {
            console.error("未找到VIew " + viewname);
            return;
        }
        this.m_pushviewStack.push(panel);
        panel.OnEnter(param);
    }


    public PopView() {
        if (this.m_pushviewStack.length <= 0) return;

        //关闭栈顶页面的显示
        let topPanel = this.m_pushviewStack.pop();
        topPanel.OnExit();

        if (this.m_pushviewStack.length <= 0) return;
        let topPanel2 = this.m_pushviewStack[this.m_pushviewStack.length - 1];
        topPanel2.OnResume();
    }


    private GetView(viewName: string): MyViewBase {
        let view: any = this.m_pushviewMap[viewName];
        if (view == null) {
            if (UIManager.sViewMap[viewName] != null) {
                view = new UIManager.sViewMap[viewName];
                this.m_pushviewMap[viewName] = view;
            }
            else {
                console.log("Woring:" + viewName + " 没有对应的View界面");
            }
        }
        return view;
    }



    //=============================================InnerView==================================================


    ShowInterView(viewName, param?) 
    {
        if (this.m_innerviewMap[viewName] == null) 
        {
            if (UIManager.sViewMap[viewName] != null) 
            {
              let view = new UIManager.sViewMap[viewName];
                this.m_innerviewMap[viewName]= view;
            }
        }
        this.m_innerviewMap[viewName].Show(param);
    }

    HideInterView(viewName) {
        if (this.m_innerviewMap[viewName] == null) {
            console.error("not found innerView " + viewName);
            return;
        }
        this.m_innerviewMap[viewName].Hide();
    }

}